package com.attendcollatesys.aspect;

import com.attendcollatesys.Annotation.AutoNameFill;
import com.attendcollatesys.entity.Employee;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Random;


@Aspect
@Component
public class AutoFillAop {
    @Pointcut("within(com.attendcollatesys.service.impl..*)")
    public void autoAnnotatedMethods() {
        // 方法体为空，因为这只是定义切入点
    }



    @Before("autoAnnotatedMethods()")
    public void setEmpName(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        AutoNameFill annotation = methodSignature.getMethod().getAnnotation(AutoNameFill.class);
        String value = annotation.value();


        Random random = new Random();
        Object[] args = joinPoint.getArgs();
        Class<?> aClass = args.getClass();
        Method setName = aClass.getMethod("setName", String.class);
        setName.invoke(args, value +"Auto"+random.nextInt());

    }
}
